package org.dreamwork.jasmine2.web.controls.chart;

import org.dreamwork.jasmine2.annotation.WebControlTemplate;
import org.dreamwork.jasmine2.util.StringConverter;
import org.dreamwork.jasmine2.web.controls.WebContainer;
import org.dreamwork.jasmine2.web.controls.chart.format.IChartDataFormat;
import org.dreamwork.jasmine2.web.controls.chart.format.LineDataFormat;
import org.dreamwork.jasmine2.web.controls.chart.settings.ChartSetting;
import org.dreamwork.jasmine2.web.controls.chart.settings.ChartType;
import org.dreamwork.jasmine2.web.controls.chart.settings.LineChartSetting;
import org.dreamwork.jasmine2.web.controls.chart.settings.template.LineChartTemplate;
import org.dreamwork.peony.DataBinder;
import org.dreamwork.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-7-30
 * Time: 14:49:43
 */
@WebControlTemplate (LineChartTemplate.class)
public class LineChart extends AbstractChartControl{
    private String categoryField, categoryColorField;
    private String maxYLeft, maxYRight, minYLeft, minYRight;

    public LineChart () {
        title = "Line & Area Chart";
    }

    public String getCategoryField () {
        return categoryField;
    }

    public void setCategoryField (String categoryField) {
        this.categoryField = categoryField;
    }

    public String getCategoryColorField () {
        return categoryColorField;
    }

    public void setCategoryColorField (String categoryColorField) {
        this.categoryColorField = categoryColorField;
    }

    @Override
    protected String parseDataAsXML () {
        IChartDataFormat cdf = new LineDataFormat ();
        ChartContext ctx = new ChartContext ();
        ctx.addParameter ("textField", textField);
        ctx.addParameter ("valueField", valueField);
        ctx.addParameter ("categoryField", categoryField);
        if (!StringConverter.isEmpty (categoryColorField))
            ctx.addParameter ("categoryColorField", categoryColorField);
        if (!StringConverter.isEmpty (url))
            ctx.addParameter ("url", url);

        List<?> data = DataBinder.parseDataSource (dataSource);
        return cdf.format (data, ctx);
    }

    @Override
    protected String getSWFName () {
        return "line";
    }

    @Override
    protected ChartSetting getSetting () {
        LineChartSetting setting = new LineChartSetting ();
        if (!StringUtil.isEmpty (minYLeft))
            setting.addSection ("values", true)
                   .addSection ("y_left", true)
                   .addAttribute ("min", minYLeft);
        if (!StringUtil.isEmpty (maxYLeft))
            setting.addSection ("values", true)
                   .addSection ("y_left", true)
                   .addAttribute ("max", maxYLeft);
        if (!StringUtil.isEmpty (minYRight))
            setting.addSection ("values", true)
                   .addSection ("y_right", true)
                   .addAttribute ("min", minYRight);
        if (!StringUtil.isEmpty (maxYLeft))
            setting.addSection ("values", true)
                   .addSection ("y_right", true)
                   .addAttribute ("max", maxYRight);
        return setting;
    }

    @Override
    protected ChartType getChartType () {
        return ChartType.Line;
    }

    @Override
    protected String getServletParameters () {
        StringBuilder builder = new StringBuilder ();
        builder.append ("r=").append (getChartType ());
        builder.append ("&p=").append (getProviderKey ());
        builder.append ("&v=").append (valueField);
        builder.append ("&t=").append (textField);
        builder.append ("&c=").append (categoryField);
        if (!StringConverter.isEmpty (categoryColorField)) builder.append ("&cc=").append (categoryColorField);
        if (!StringConverter.isEmpty (url)) builder.append ("&u=").append (url);

        HttpServletRequest request = page.getHttpContext ().getRequest ();
        for (Enumeration e = request.getParameterNames (); e.hasMoreElements ();) {
            String key = (String) e.nextElement ();
            if (key.startsWith ("__")) continue;
            String[] values = request.getParameterValues (key);
            if (values.length > 1) {
                for (String v : values) builder.append ('&').append (key).append ('=').append (v);
            } else {
                builder.append ('&').append (key).append ('=').append (values [0]);
            }
        }
        return builder.toString ();
    }

    public String getMaxYLeft () {
        return maxYLeft;
    }

    public void setMaxYLeft (String maxYLeft) {
        this.maxYLeft = maxYLeft;
    }

    public String getMaxYRight () {
        return maxYRight;
    }

    public void setMaxYRight (String maxYRight) {
        this.maxYRight = maxYRight;
    }

    public String getMinYLeft () {
        return minYLeft;
    }

    public void setMinYLeft (String minYLeft) {
        this.minYLeft = minYLeft;
    }

    public String getMinYRight () {
        return minYRight;
    }

    public void setMinYRight (String minYRight) {
        this.minYRight = minYRight;
    }
}